package com.example.leero.graduation.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;

import com.example.leero.graduation.API;
import com.example.leero.graduation.App;
import com.example.leero.graduation.R;
import com.example.leero.graduation.adapter.FavourSportAdapter;
import com.example.leero.graduation.customView.MyGridView;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import okhttp3.Call;

public class FavourSportActivity extends AppCompatActivity {

    private MyGridView gv_screen_type;
    private FavourSportAdapter screen_type_adapter;
    private Boolean[] selected = {false, false, false, false, false, false, false};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_favour_sport);

        TextView title = (TextView) findViewById(R.id.tv_title);
        title.setText("关注的活动");
        findViewById(R.id.iv_left).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        gv_screen_type = (MyGridView) findViewById(R.id.gv_screen_type);
        initGridView();

        findViewById(R.id.btn_submit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                postSelection();
            }
        });
    }

    private void initGridView() {
        String[] type = {"跑步", "羽毛球", "篮球", "足球", "网球", "乒乓球", "自行车"};
        String[] startData = App.getLoginUser().favorite_sport.split(",");
        for(int n = 0; n < startData.length; n++) {
            int position = Integer.parseInt(startData[n]);
            if (position == 0) continue;
            selected[position - 1] = true;
        }

        screen_type_adapter = new FavourSportAdapter(this, type);
        screen_type_adapter.setSelected(selected);
        gv_screen_type.setAdapter(screen_type_adapter);

        gv_screen_type.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                for(int n=0; n<selected.length; n++){
                    if(n == position && selected[position]) {
                        selected[position] = false;
                    } else if(n == position && !selected[position]) {
                        selected[position] = true;
                    }
                }
                screen_type_adapter.setSelected(selected);
                screen_type_adapter.notifyDataSetChanged();
            }
        });
    }

    private void postSelection() {
        String selections = "";
        for(int n = 1 ; n <= selected.length; n++) {
            if (selected[n-1]) {
                selections = selections + n + ",";
            }
        }

        OkHttpUtils.post().url(API.UPDATE_FAVOUT_SPORT)
                .addParams("user_id", App.getLoginUser().id+"")
                .addParams("sports", selections)
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
            }
            @Override
            public void onResponse(String response, int id) {
                FavourSportActivity.this.finish();
            }
        });
    }
}
